package cn.wolfcode.crm.web.Controller;

import cn.wolfcode.crm.Service.ISystemDictionaryService;
import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.query.SystemDictionaryQuery;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//shiro需要在用户访问响应资源的时候对其权限做检查
//因为需要在访问每个资源的时候都做检查,所以需要使用通用的代码
//shiro使用动态代理的方式来实现
//动态代理的使用场景:对目标方法做功能增强的时候(原本没有的功能.增强的时候赋予)
@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //shiro中的value只是权限表达式,但我们将用作其他用途
    //RequiresPermissions:当前方法是否用户value中指定的的所有权限
    //如果有,运行访问,否则不能访问
    //logical:检查权限的时候,是都需要(AND),还是有一个就行(OR)
    @RequiresPermissions(value = {"数据字典列表","systemDictionary:list"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryQuery qo) {
        PageInfo<SystemDictionary> pageResult = systemDictionaryService.queryForList(qo);
        model.addAttribute("pageResult", pageResult);
        return "systemDictionary/list";//跳转到list.jsp页面/WEB-INF/view/systemDictionary/list.flt
    }

    @RequiresPermissions(value = {"数据字典保存或修改","systemDictionary:saveOrUpdate"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary) {
        JsonResult result = new JsonResult();
        try {
            if (systemDictionary.getId() == null) {
                systemDictionaryService.insert(systemDictionary);
            } else {
                systemDictionaryService.updateByPrimaryKey(systemDictionary);
            }
        } catch (Exception e) {
                e.printStackTrace();
                result.mark("保存失败");
            }

        return result;
    }


}
